package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qf.commons.annotation.UserLogin;
import com.qf.entity.Dept;
import com.qf.entity.Emp;
import com.qf.entity.PageInfo;
import com.qf.service.DeptService;
import com.qf.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: key
 * ClassName:EmpController
 * Date: 2023/5/25
 * Time: 16:36
 * description:
 */
@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpService empService;
    @Autowired
    private DeptService deptService;

    @RequestMapping("/list")
    public String empList(Model model) throws SQLException {
        List<Emp> emps = empService.selectAll();
        model.addAttribute("empList", emps);
        return "emp/empList";
    }

    @RequestMapping(value = "/deptMsg")
    public String deptMsg(Model model) {
        List<Dept> depts = deptService.selectDept();
        model.addAttribute("deptList", depts);
        return "emp/addEmp";
    }

    @RequestMapping(value = "/addEmp", method = RequestMethod.POST)
    public String addEmp(Emp emp) throws SQLException {
        Integer integer = empService.insert(emp);
        return "redirect:list";
    }

    @RequestMapping(value = "/deleteEmp", method = RequestMethod.GET)
    public String deleteEmp(@RequestParam(value = "eno", required = false, defaultValue = "1") Integer eno) throws SQLException {
        empService.delete(eno);
        return "redirect:list";
    }

    @RequestMapping(value = "/updateEmp")
    public String updateEmp(@RequestParam(value = "eno", required = false, defaultValue = "1") Integer eno, Model model) throws SQLException {
        Emp emp = empService.select(eno);
        System.out.println(eno);
        model.addAttribute("oneEmp", emp);
        List<Dept> depts = deptService.selectDept();
        model.addAttribute("deptList", depts);
        return "emp/updateEmp";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Emp emp) throws SQLException {

        System.out.println(emp.getEno() + "-----");
        Integer update = empService.update(emp);
        System.out.println(update);
        return "redirect:list";
    }

    @RequestMapping("/test")
    @UserLogin(required = true)
    public String test(Emp emp) {
        System.out.println("emp = " + emp);
        return "index";
    }



    @ResponseBody
    @RequestMapping(value = "/deleteBench")
    public Map<String,Object> deleteBench(@RequestBody List<Integer> ids) throws SQLException {
        System.out.println(ids);
        List<Emp> emps = empService.selectByEnos(ids);
        Integer integer = empService.deleteByIds(emps);
        System.out.println(integer);
        Map<String,Object> result = new HashMap<>();
        result.put("success",ids);
        return result;
    }
}
